﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.Native;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.EventBroker;
using NetROS.Tiers.Data;
using NetROS.Tiers.Data.Bases;
using NetROS.Tiers.Entities;
using NetRosApp.Infrastructure.Interface.Base;
using DevExpress.XtraReports.UI;
using NetRosApp.PlaPart.Constants;
using NetRosApp.Infrastructure.Interface;
using NetROS.Tiers.Services;
using NetRosApp.PlaPart.Views.BaseEditors;
using ReportiPla;


namespace NetRosApp.PlaPart
{

    public class OpenIzvjestaj
    {
        private int impersinate = 0;
        private int brojkopija = 1;
        public void ShowReportForEntity(Object sender, Object entitet, WorkItem WorkItem)
        {
            XtraReport _report = null;
            string naziv = string.Empty;
            int tipispisa = 0;
            ReportSelectorFrm _tmpfrm;
            System.Collections.ArrayList _tmpList;
            brojkopija = 1;

            if ((sender is ObracunViewPresenter || sender is ObracunTraziPresenter))
            {
            
                _tmpfrm = new ReportSelectorFrm();
                _tmpList = new System.Collections.ArrayList();
                _tmpList.Add("Isplatna lista");
                _tmpList.Add("RSM Stranica A");
                _tmpList.Add("RSM Stranica B");
                _tmpList.Add("Specifikacija po bankama");
                _tmpList.Add("Potpisna lista");
                _tmpList.Add("IDObrazac 2011");
                _tmpList.Add("IDObrazac 2011 novi");
                _tmpList.Add("IDObrazac 2011 vrsta 11");
                _tmpList.Add("IDObrazac 2010");
                _tmpList.Add("IPP");
                _tmpList.Add("Rekapitulacija obračuna");
                _tmpfrm.LBIzabir.DataSource = _tmpList;
                _tmpfrm.LBIzabir.SelectedIndex = 0;
                if (_tmpfrm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Isplatna lista")
                    {
                        if (entitet as VwObracunTrazi != null)
                        {
                            entitet = WorkItem.Services.Get<ObracunService>().GetByID(((VwObracunTrazi) entitet).ID);
                            WorkItem.Services.Get<ObracunService>().DeepLoad((Obracun) entitet, true,
                                                                             DeepLoadType.IncludeChildren,
                                                                             typeof (TList<ObracunOsoba>));
                            foreach (ObracunOsoba obracunOsoba in ((Obracun) entitet).ObracunOsobaCollection)
                            {
                                WorkItem.Services.Get<ObracunOsobaService>().DeepLoad(obracunOsoba, true,
                                                                                      DeepLoadType.IncludeChildren,
                                                                                      typeof(Obracun),
                                                                                      typeof (
                                                                                          TList
                                                                                          <ObracunOsobaPoreznaOlaksica>)
                                                                                      ,
                                                                                      typeof (
                                                                                          TList
                                                                                          <ObracunOsobaObustavaNaknada>)
                                                                                      ,
                                                                                      typeof (
                                                                                          TList<ObracunOsobaPrimanje>)
                                    );
                            }
                        }

                        if (entitet as Obracun != null)
                        {
                            OsobaAdreseParameterBuilder o = new OsobaAdreseParameterBuilder();
                                    o.AppendEquals("IDAdresaTip", "2");
                                    o.AppendLessThanOrEqual("DatumOd", ((Obracun)entitet).DatumDo.ToString("yyyyMMdd"));
                                    o.AppendTekst(" and coalesce(DatumDo,'20991231') >= '" + ((Obracun)entitet).DatumDo.ToString("yyyyMMdd") + "'");
                                    TList<OsobaAdrese> oa = new TList<OsobaAdrese>();
                                    oa = WorkItem.Services.Get<OsobaAdreseService>().Find(o.GetParameters());
                            foreach (OsobaAdrese osobaAdrese in oa)
                            {
                                 osobaAdrese.IDMjestoSource= WorkItem.Services.Get<MjestoService>().GetByID(osobaAdrese.IDMjesto);
                            }

                            OsobaBankovniRacunParameterBuilder oPB = new OsobaBankovniRacunParameterBuilder();
                            oPB.AppendTekst(" coalesce(DatumOd,'19000101') <= '" + ((Obracun)entitet).DatumDo.ToString("yyyyMMdd") + "' AND COALESCE(DatumDo,'20991231') >= '" + ((Obracun)entitet).DatumDo.ToString("yyyyMMdd") + "'");
                            TList<OsobaBankovniRacun> obr = new TList<OsobaBankovniRacun>();
                                    obr = WorkItem.Services.Get<OsobaBankovniRacunService>().Find(oPB.GetParameters());

                            foreach (ObracunOsoba obracunOsoba in ((Obracun) entitet).ObracunOsobaCollection)
                            {
                                WorkItem.Services.Get<ObracunOsobaService>().DeepLoad(obracunOsoba, true,
                                                                                      DeepLoadType.IncludeChildren, typeof(Obracun),
                                                                                      typeof (Osoba));
                                if (obracunOsoba.IDOsobaSource != null)
                                {
                                    //WorkItem.Services.Get<OsobaService>().DeepLoad(obracunOsoba.IDOsobaSource, true,
                                    //                                               DeepLoadType.IncludeChildren,
                                    //                                               typeof(OsobaBankovniRacun));
                                    
                                    obracunOsoba.IDOsobaSource.OsobaBankovniRacunCollection = obr.FindAll("IDOsoba", obracunOsoba.IDOsoba);
                                }
                                obracunOsoba.IDOsobaSource.OsobaAdreseCollection = oa.FindAll("IDOsoba",obracunOsoba.IDOsoba);
                                
                                foreach (
                                    ObracunOsobaPoreznaOlaksica olaksica in
                                        obracunOsoba.ObracunOsobaPoreznaOlaksicaCollection)
                                {
                                    WorkItem.Services.Get<ObracunOsobaPoreznaOlaksicaService>().DeepLoad(olaksica,
                                                                                                         true,
                                                                                                         DeepLoadType
                                                                                                             .
                                                                                                             IncludeChildren
                                                                                                         ,
                                                                                                         typeof (
                                                                                                             OsobaPoreznaOlaksica
                                                                                                             ));
                                }
                                foreach (
                                    ObracunOsobaPrimanje osobaPrimanje in
                                        obracunOsoba.ObracunOsobaPrimanjeCollection)
                                {
                                    WorkItem.Services.Get<ObracunOsobaPrimanjeService>().DeepLoad(osobaPrimanje,
                                                                                                  true,
                                                                                                  DeepLoadType.
                                                                                                      IncludeChildren,
                                                                                                  typeof (
                                                                                                      PrimanjeTip));
                                }
                                foreach (
                                    ObracunOsobaObustavaNaknada obustavaNaknada in
                                        obracunOsoba.ObracunOsobaObustavaNaknadaCollection)
                                {
                                    WorkItem.Services.Get<ObracunOsobaObustavaNaknadaService>().DeepLoad(
                                        obustavaNaknada, true,
                                        DeepLoadType.
                                            IncludeChildren,
                                        typeof (
                                            ObustavaNaknadaTip));
                                }
                            }
                        }
                        //((Obracun)entitet).ObracunOsobaCollection.Sort("OsobaSourcePrezimeIme ASC");
                        if (((Obracun)entitet).DatumIsplate <= new DateTime(2010, 6, 30))
                        {
                            _report = new IsplatnaLista((Obracun)entitet, WorkItem.Services.Get<SifarnikService>().GetByID(148).VrijednostTekst);
                        }
                        if (((Obracun)entitet).DatumIsplate >= new DateTime(2010, 7, 1))
                        {
                            DateTime? dosp = null;
                             
                            using (UpitPla myForm = new UpitPla())
                            {
                                
                                if (myForm.ShowDialog()==DialogResult.Yes)
                                {
                                    dosp = myForm.dateEdit1.EditValue as DateTime?;
                                }
                            }

                            _report = new IsplatnaLista2((Obracun)entitet, WorkItem.Services.Get<SifarnikService>().GetByID(148).VrijednostTekst, WorkItem.Services.Get<SifarnikService>().GetByID(149).VrijednostTekst, WorkItem.Services.Get<SifarnikService>().GetByID(150).VrijednostTekst, WorkItem.Services.Get<SifarnikService>().GetByID(171).VrijednostTekst,dosp);
                        }
                        _report.Name = string.Format("Isplatna lista za mjesec {0}", ((Obracun) entitet).DatumOd.Month);
                        OpenReport("Isplatna lista", _report, WorkItem, _tmpfrm.tipispisa);

                    }
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "RSM Stranica B")
                    {
                        VList<VwObracunZaRegosStavke> _rsmobv = new VList<VwObracunZaRegosStavke>();
                        int idob = 0;
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaRegosStavkeParameterBuilder _pb =
                                new VwObracunZaRegosStavkeParameterBuilder();
                            _pb.AppendEquals("ID", (entitet as VwObracunTrazi).ID.ToString());
                            idob = (entitet as VwObracunTrazi).ID;
                            _rsmobv = WorkItem.Services.Get<VwObracunZaRegosStavkeService>().Find(_pb.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaRegosStavkeParameterBuilder _pb =
                                             new VwObracunZaRegosStavkeParameterBuilder();
                            _pb.AppendEquals("ID", (entitet as Obracun).ID.ToString());
                            idob = (entitet as Obracun).ID;
                            _rsmobv = WorkItem.Services.Get<VwObracunZaRegosStavkeService>().Find(_pb.GetParameters());
                        }
                        if (_rsmobv.Count >= 0)
                        {
                            _report = new RSMstrB(_rsmobv);
                            _report.Name = string.Format("RsmObrazac B {0}", idob);
                            OpenReport("RSM Stranica B", _report, WorkItem, _tmpfrm.tipispisa);
                        }
                    }
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Specifikacija po bankama")
                    {
                        VList<VwObracunSpecifikacijaPoBankama> _rsmobv = new VList<VwObracunSpecifikacijaPoBankama>();
                        int idob = 0;
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunSpecifikacijaPoBankamaParameterBuilder _pb =
                                new VwObracunSpecifikacijaPoBankamaParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            idob = (entitet as VwObracunTrazi).ID;
                            _rsmobv = WorkItem.Services.Get<VwObracunSpecifikacijaPoBankamaService>().Find(_pb.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunSpecifikacijaPoBankamaParameterBuilder _pb =
                                             new VwObracunSpecifikacijaPoBankamaParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            idob = (entitet as Obracun).ID;
                            _rsmobv = WorkItem.Services.Get<VwObracunSpecifikacijaPoBankamaService>().Find(_pb.GetParameters());
                        }
                        if (_rsmobv.Count >= 0)
                        {
                            _report = new SpecifikacijaPoBankama(_rsmobv, WorkItem.Services.Get<SifarnikService>().GetByID(148).VrijednostTekst);
                            _report.Name = string.Format("Spec. po bankama {0}", idob);
                            OpenReport("Spec. po bankama", _report, WorkItem, _tmpfrm.tipispisa);
                        }
                    }
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Potpisna lista")
                    {
                        if (entitet as VwObracunTrazi != null)
                        {
                            entitet = WorkItem.Services.Get<ObracunService>().GetByID(((VwObracunTrazi) entitet).ID);
                            WorkItem.Services.Get<ObracunService>().DeepLoad((Obracun) entitet, true,
                                                                             DeepLoadType.IncludeChildren,
                                                                             typeof (TList<ObracunOsoba>));

                        }
                    

                        if (entitet as Obracun != null)
                        {
                            foreach (ObracunOsoba obracunOsoba in ((Obracun)entitet).ObracunOsobaCollection)
                            {
                                WorkItem.Services.Get<ObracunOsobaService>().DeepLoad(obracunOsoba, true,
                                                                                      DeepLoadType.IncludeChildren, typeof(Obracun),
                                                                                      typeof(Osoba));
                                if (obracunOsoba.IDOsobaSource != null)
                                {
                                    WorkItem.Services.Get<OsobaService>().DeepLoad(obracunOsoba.IDOsobaSource, true,
                                                                                   DeepLoadType.IncludeChildren,
                                                                                   typeof(OsobaBankovniRacun));
                                }
                            }
                        }
                        _report = new PotpisnaLista((Obracun)entitet, WorkItem.Services.Get<SifarnikService>().GetByID(148).VrijednostTekst);
                             _report.Name = string.Format("Potpisna lista {0}", ((Obracun)entitet).ID);
                                OpenReport("Potpisna lista", _report, WorkItem, _tmpfrm.tipispisa);
                        
                    }
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Rekapitulacija obračuna")
                    {
                        if (entitet as VwObracunTrazi != null)
                        {
                            entitet = WorkItem.Services.Get<ObracunService>().GetByID(((VwObracunTrazi)entitet).ID);
                            WorkItem.Services.Get<ObracunService>().DeepLoad((Obracun)entitet, true,
                                                                             DeepLoadType.IncludeChildren,
                                                                             typeof(TList<ObracunOsoba>));

                        }


                        if (entitet as Obracun != null)
                        {
                            foreach (ObracunOsoba obracunOsoba in ((Obracun)entitet).ObracunOsobaCollection)
                            {
                                WorkItem.Services.Get<ObracunOsobaService>().DeepLoad(obracunOsoba, true,
                                                                                      DeepLoadType.IncludeChildren, typeof(Obracun),
                                                                                      typeof(Osoba));
                                if (obracunOsoba.IDOsobaSource != null)
                                {
                                    WorkItem.Services.Get<OsobaService>().DeepLoad(obracunOsoba.IDOsobaSource, true,
                                                                                   DeepLoadType.IncludeChildren,
                                                                                   typeof(OsobaBankovniRacun));
                                }
                            }
                        }
                        if (((Obracun)entitet).DatumIsplate <= new DateTime(2010, 6, 30))
                        {
                            _report = new RekapitulacijaObracuna((Obracun)entitet, WorkItem.Services.Get<SifarnikService>().GetByID(148).VrijednostTekst);
                        }
                        if (((Obracun)entitet).DatumIsplate >= new DateTime(2010, 7, 1))
                        {
                            _report = new RekapitulacijaObracuna2((Obracun)entitet, WorkItem.Services.Get<SifarnikService>().GetByID(148).VrijednostTekst);
                        }
                        _report.Name = string.Format("Rekapitulacija obračuna {0}", ((Obracun)entitet).ID);
                        OpenReport("Rekapitulacija obračuna", _report, WorkItem, _tmpfrm.tipispisa);

                    }

                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "RSM Stranica A")
                    {
                        VList<VwObracunZaID> _rsmobv = new VList<VwObracunZaID>();
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());
                        }
                        if (_rsmobv.Count == 1)
                        {
                            VwObracunZaID _rsmob = _rsmobv[0];
                            _report = new RSMStrA(_rsmob);
                            _report.Name = string.Format("RsmObrazac A{0}", (_rsmob.IDObracun));
                            OpenReport("RSM Stranica A", _report, WorkItem, _tmpfrm.tipispisa);
                        }
                    }
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "IPP")
                    {
                        VList<VwObracunZaID> _rsmobv = new VList<VwObracunZaID>();
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());
                        }
                        if (_rsmobv.Count == 1)
                        {
                            VwObracunZaID _rsmob = _rsmobv[0];
                            _report = new ObrazacIPP(_rsmob);
                            _report.Name = string.Format("ObrazacIPP {0}", (_rsmob.IDObracun));
                            OpenReport("Obrazac IPP", _report, WorkItem, _tmpfrm.tipispisa);
                        }
                    }
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "IDObrazac 2010")
                    {
                        VList<VwObracunZaID> _rsmobv = new VList<VwObracunZaID>();
                        VList<VwObracunZaIDPorezPrirez> _rsmobvpp = new VList<VwObracunZaIDPorezPrirez>();
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezParameterBuilder _pb2 =
                             new VwObracunZaIDPorezPrirezParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezService>().Find(_pb2.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezParameterBuilder _pb2 =
                            new VwObracunZaIDPorezPrirezParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezService>().Find(_pb2.GetParameters());

                        }
                        ObrazacIDZajedno _obZaj = new ObrazacIDZajedno();
                        ObrazacID _obID = null;
                        ObrazacIDstr2 _obID2 = null;
                        if (_rsmobv.Count == 1)
                        {
                            VwObracunZaID _rsmob = _rsmobv[0];
                            _obID = new ObrazacID(_rsmob);
                            _obID.Name = string.Format("ObrazacID {0}", (_rsmob.IDObracun));
                        }
                                                    
                     
                        if (_obID != null)
                        {
                            _obID2 = new ObrazacIDstr2(_rsmobvpp);
                            _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                            _obID.CreateDocument();
                         //   _obID.BeforePrint += new PrintEventHandler(_obID_BeforePrint);
                            _obID.AfterPrint += delegate(object snd, EventArgs e)
                                                    {
                                                        XtraReport _obIDParent = ((XtraReport)snd);
                                                        _obID2 = new ObrazacIDstr2( _rsmobvpp);
                                                        _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                                                        _obID2.Margins = _obIDParent.Margins;
                                                        _obID2.PrinterName =  _obIDParent.PrintingSystem.PageSettings.PrinterName;
                                                        //_report.PrintingSystem.PageSettings.PrinterName = printertoprint;
                                                        _obID2.PrintingSystem.StartPrint += PrintingSystem_StartPrint;

                                                        _obID2.PrintingSystem.ShowMarginsWarning = false;
                                                        _obID2.PaperKind =
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind;
                                                        _obID2.PaperName =  _obIDParent.PrintingSystem.PageSettings.PaperName;
                                                        _obID2.PrintingSystem.PageSettings.Assign(
                                                            _obIDParent.PrintingSystem.PageSettings.Margins,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperName, _obIDParent.PrintingSystem.PageSettings.Landscape);
                                                        _obID2.PrintingSystem.PageSettings.Landscape = _obIDParent.PrintingSystem.PageSettings.Landscape;
                                                        //_obID2.PrintingSystem.PageSettings.Landscape = true;
                                                        _obID2.Margins = _obIDParent.PrintingSystem.PageSettings.Margins;
                                                   //     _obID2.PrintingSystem = _obID.PrintingSystem;
                                                        _obID2.CreateDocument();
                                                        _obIDParent.Pages.AddRange(_obID2.Pages);
                                                 
                                        
                                                    };
                     
                            //_obZaj.xrSubreport1.ReportSource = _obID;
                            //_obZaj.xrSubreport2.ReportSource = _obID2;
                            //_report = _obZaj;
                            _report = _obID;

                            OpenReport("ObrazacID", _report, WorkItem, _tmpfrm.tipispisa);
                        }



                        
                        

                    }

                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "IDObrazac 2011")
                    {
                        VList<VwObracunZaID> _rsmobv = new VList<VwObracunZaID>();
                        VList<VwObracunZaIDPorezPrirez> _rsmobvpp = new VList<VwObracunZaIDPorezPrirez>();
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezParameterBuilder _pb2 =
                             new VwObracunZaIDPorezPrirezParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezService>().Find(_pb2.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDParameterBuilder _pb =
                                new VwObracunZaIDParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezParameterBuilder _pb2 =
                            new VwObracunZaIDPorezPrirezParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezService>().Find(_pb2.GetParameters());

                        }
                        ObrazacIDZajedno _obZaj = new ObrazacIDZajedno();
                        ObrazacID2011 _obID = null;
                        ObrazacIDstr2 _obID2 = null;

                        
                        if (_rsmobv.Count == 1)
                        {
                            VwObracunZaID _rsmob = _rsmobv[0];
                            _obID = new ObrazacID2011(_rsmob);
                            _obID.Name = string.Format("ObrazacID {0}", (_rsmob.IDObracun));
                        }
                                                    
                     
                        if (_obID != null)
                        {
                            _obID2 = new ObrazacIDstr2(_rsmobvpp);
                            _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                            _obID.CreateDocument();
                         //   _obID.BeforePrint += new PrintEventHandler(_obID_BeforePrint);
                            _obID.AfterPrint += delegate(object snd, EventArgs e)
                                                    {
                                                        XtraReport _obIDParent = ((XtraReport)snd);
                                                        _obID2 = new ObrazacIDstr2( _rsmobvpp);
                                                        _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                                                        _obID2.Margins = _obIDParent.Margins;
                                                        _obID2.PrinterName =  _obIDParent.PrintingSystem.PageSettings.PrinterName;
                                                        //_report.PrintingSystem.PageSettings.PrinterName = printertoprint;
                                                        _obID2.PrintingSystem.StartPrint += PrintingSystem_StartPrint;

                                                        _obID2.PrintingSystem.ShowMarginsWarning = false;
                                                        _obID2.PaperKind =
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind;
                                                        _obID2.PaperName =  _obIDParent.PrintingSystem.PageSettings.PaperName;
                                                        _obID2.PrintingSystem.PageSettings.Assign(
                                                            _obIDParent.PrintingSystem.PageSettings.Margins,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperName, _obIDParent.PrintingSystem.PageSettings.Landscape);
                                                        _obID2.PrintingSystem.PageSettings.Landscape = _obIDParent.PrintingSystem.PageSettings.Landscape;
                                                        //_obID2.PrintingSystem.PageSettings.Landscape = true;
                                                        _obID2.Margins = _obIDParent.PrintingSystem.PageSettings.Margins;
                                                   //     _obID2.PrintingSystem = _obID.PrintingSystem;
                                                        _obID2.CreateDocument();
                                                        _obIDParent.Pages.AddRange(_obID2.Pages);
                                                 
                                        
                                                    };
                     
                            //_obZaj.xrSubreport1.ReportSource = _obID;
                            //_obZaj.xrSubreport2.ReportSource = _obID2;
                            //_report = _obZaj;
                            _report = _obID;

                            OpenReport("ObrazacID 2011", _report, WorkItem, _tmpfrm.tipispisa);
                        }



                        
                        

                    }
                         if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "IDObrazac 2011 novi")
                    {
                        VList<VwObracunZaIDPoObrascu> _rsmobv = new VList<VwObracunZaIDPoObrascu>();
                        VList<VwObracunZaIDPorezPrirezPoObrascu> _rsmobvpp = new VList<VwObracunZaIDPorezPrirezPoObrascu>();
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPoObrascuParameterBuilder _pb =
                                new VwObracunZaIDPoObrascuParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _pb.AppendNotEquals("ObrazacIndentifikator", "11");
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDPoObrascuService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezPoObrascuParameterBuilder _pb2 =
                             new VwObracunZaIDPorezPrirezPoObrascuParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _pb2.AppendNotEquals("ObrazacIndentifikator", "11");
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezPoObrascuService>().Find(_pb2.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPoObrascuParameterBuilder _pb =
                                new VwObracunZaIDPoObrascuParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _pb.AppendNotEquals("ObrazacIndentifikator", "11");
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDPoObrascuService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezPoObrascuParameterBuilder _pb2 =
                            new VwObracunZaIDPorezPrirezPoObrascuParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _pb2.AppendNotEquals("ObrazacIndentifikator", "11");
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezPoObrascuService>().Find(_pb2.GetParameters());

                        }
                        ObrazacIDZajedno _obZaj = new ObrazacIDZajedno();
                        ObrazacID2011 _obID = null;
                        ObrazacIDstr2 _obID2 = null;

                        
                        if (_rsmobv.Count == 1)
                        {
                            VwObracunZaIDPoObrascu _rsmob = _rsmobv[0];
                            //_rsmob.ObrazacIndentifikator = "11";
                            //_rsmob.IdentifikatorIDSifra = "11";
                            _obID = new ObrazacID2011(_rsmob);
                            _obID.Name = string.Format("ObrazacID {0}", (_rsmob.IDObracun));
                            
                        }
                                                    
                     
                        if (_obID != null)
                        {
                            _obID2 = new ObrazacIDstr2(_rsmobvpp);
                            _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                            _obID.CreateDocument();
                         //   _obID.BeforePrint += new PrintEventHandler(_obID_BeforePrint);
                            _obID.AfterPrint += delegate(object snd, EventArgs e)
                                                    {
                                                        XtraReport _obIDParent = ((XtraReport)snd);
                                                        _obID2 = new ObrazacIDstr2( _rsmobvpp);
                                                        _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                                                        _obID2.Margins = _obIDParent.Margins;
                                                        _obID2.PrinterName =  _obIDParent.PrintingSystem.PageSettings.PrinterName;
                                                        //_report.PrintingSystem.PageSettings.PrinterName = printertoprint;
                                                        _obID2.PrintingSystem.StartPrint += PrintingSystem_StartPrint;

                                                        _obID2.PrintingSystem.ShowMarginsWarning = false;
                                                        _obID2.PaperKind =
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind;
                                                        _obID2.PaperName =  _obIDParent.PrintingSystem.PageSettings.PaperName;
                                                        _obID2.PrintingSystem.PageSettings.Assign(
                                                            _obIDParent.PrintingSystem.PageSettings.Margins,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperName, _obIDParent.PrintingSystem.PageSettings.Landscape);
                                                        _obID2.PrintingSystem.PageSettings.Landscape = _obIDParent.PrintingSystem.PageSettings.Landscape;
                                                        //_obID2.PrintingSystem.PageSettings.Landscape = true;
                                                        _obID2.Margins = _obIDParent.PrintingSystem.PageSettings.Margins;
                                                   //     _obID2.PrintingSystem = _obID.PrintingSystem;
                                                        _obID2.CreateDocument();
                                                        _obIDParent.Pages.AddRange(_obID2.Pages);
                                                 
                                        
                                                    };
                     
                            //_obZaj.xrSubreport1.ReportSource = _obID;
                            //_obZaj.xrSubreport2.ReportSource = _obID2;
                            //_report = _obZaj;
                            _report = _obID;

                            OpenReport("IDObrazac 2011 novi", _report, WorkItem, _tmpfrm.tipispisa);
                        }



                        
                        

                    }

                     if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "IDObrazac 2011 vrsta 11")
                    {
                        VList<VwObracunZaIDPoObrascu> _rsmobv = new VList<VwObracunZaIDPoObrascu>();
                        VList<VwObracunZaIDPorezPrirezPoObrascu> _rsmobvpp = new VList<VwObracunZaIDPorezPrirezPoObrascu>();
                        if (entitet as VwObracunTrazi != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPoObrascuParameterBuilder _pb =
                                new VwObracunZaIDPoObrascuParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _pb.AppendEquals("ObrazacIndentifikator", "11");
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDPoObrascuService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezPoObrascuParameterBuilder _pb2 =
                             new VwObracunZaIDPorezPrirezPoObrascuParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as VwObracunTrazi).ID.ToString());
                            _pb2.AppendEquals("ObrazacIndentifikator", "11");
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezPoObrascuService>().Find(_pb2.GetParameters());

                        }
                        if (entitet as Obracun != null)
                        {
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPoObrascuParameterBuilder _pb =
                                new VwObracunZaIDPoObrascuParameterBuilder();
                            _pb.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _pb.AppendEquals("ObrazacIndentifikator", "11");
                            _rsmobv = WorkItem.Services.Get<VwObracunZaIDPoObrascuService>().Find(_pb.GetParameters());
                            NetROS.Tiers.Data.Bases.VwObracunZaIDPorezPrirezPoObrascuParameterBuilder _pb2 =
                            new VwObracunZaIDPorezPrirezPoObrascuParameterBuilder();
                            _pb2.AppendEquals("IDObracun", (entitet as Obracun).ID.ToString());
                            _pb2.AppendEquals("ObrazacIndentifikator", "11");
                            _rsmobvpp = WorkItem.Services.Get<VwObracunZaIDPorezPrirezPoObrascuService>().Find(_pb2.GetParameters());

                        }
                        ObrazacIDZajedno _obZaj = new ObrazacIDZajedno();
                        ObrazacID2011 _obID = null;
                        ObrazacIDstr2 _obID2 = null;

                        
                        if (_rsmobv.Count == 1)
                        {
                            VwObracunZaIDPoObrascu _rsmob = _rsmobv[0];
                            _rsmob.ObrazacIndentifikator = "11";
                            _rsmob.IdentifikatorIDSifra = "11";
                            _obID = new ObrazacID2011(_rsmob);
                            _obID.Name = string.Format("ObrazacID {0}", (_rsmob.IDObracun));
                            
                        }
                                                    
                     
                        if (_obID != null)
                        {
                            _obID2 = new ObrazacIDstr2(_rsmobvpp);
                            _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                            _obID.CreateDocument();
                         //   _obID.BeforePrint += new PrintEventHandler(_obID_BeforePrint);
                            _obID.AfterPrint += delegate(object snd, EventArgs e)
                                                    {
                                                        XtraReport _obIDParent = ((XtraReport)snd);
                                                        _obID2 = new ObrazacIDstr2( _rsmobvpp);
                                                        _obID2.Name = string.Format("ObrazacIDPorPri {0}", (_obID.Name));
                                                        _obID2.Margins = _obIDParent.Margins;
                                                        _obID2.PrinterName =  _obIDParent.PrintingSystem.PageSettings.PrinterName;
                                                        //_report.PrintingSystem.PageSettings.PrinterName = printertoprint;
                                                        _obID2.PrintingSystem.StartPrint += PrintingSystem_StartPrint;

                                                        _obID2.PrintingSystem.ShowMarginsWarning = false;
                                                        _obID2.PaperKind =
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind;
                                                        _obID2.PaperName =  _obIDParent.PrintingSystem.PageSettings.PaperName;
                                                        _obID2.PrintingSystem.PageSettings.Assign(
                                                            _obIDParent.PrintingSystem.PageSettings.Margins,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperKind,
                                                            _obIDParent.PrintingSystem.PageSettings.PaperName, _obIDParent.PrintingSystem.PageSettings.Landscape);
                                                        _obID2.PrintingSystem.PageSettings.Landscape = _obIDParent.PrintingSystem.PageSettings.Landscape;
                                                        //_obID2.PrintingSystem.PageSettings.Landscape = true;
                                                        _obID2.Margins = _obIDParent.PrintingSystem.PageSettings.Margins;
                                                   //     _obID2.PrintingSystem = _obID.PrintingSystem;
                                                        _obID2.CreateDocument();
                                                        _obIDParent.Pages.AddRange(_obID2.Pages);
                                                 
                                        
                                                    };
                     
                            //_obZaj.xrSubreport1.ReportSource = _obID;
                            //_obZaj.xrSubreport2.ReportSource = _obID2;
                            //_report = _obZaj;
                            _report = _obID;

                            OpenReport("IDObrazac 2011 vrsta 11", _report, WorkItem, _tmpfrm.tipispisa);
                        }



                        
                        

                    }
                
            }
              //  _report = new ReportiPla.IsplatnaLista(new Obracun());

            }

            //if (sender is KnjizenjeTraziPresenter)
            //{
            //    report = new KnjizenjeIspis();
            //    report.Name = string.Format("Ispis knjiženja");

            //    if (entitet is VList<VwKnjizenjeIspis> && ((VList<VwKnjizenjeIspis>)entitet).Count > 0 &&
            //        WorkItem.Services.Get<KnjizenjeService>().GetByID(((VList<VwKnjizenjeIspis>)entitet)[0].IDKnjiz)
            //            .IDStatus == 0)
            //    {
            //        report.Watermark.Text = "P R I V R E M E N I";
            //        report.Watermark.ShowBehind = true;
            //    }

            //    _bindingSource = report.DataSource as BindingSource;
            //    if (_bindingSource != null)
            //        _bindingSource.DataSource = entitet;
            //    //_reportZagPodnozje = new ReportZagPodnozje("","");
            //    //_reportZagPodnozje.xrSubreport1.ReportSource = report;

            //    //    _reportZagPodnozje.Name = string.Format("Ispis knjiženja");
            //    naziv = string.Format("Ispis knjiženja");
            //}
        }



        private PageSettings _last = null;
        private void OpenReport(string _naziv, XtraReport _report, WorkItem workitem, int tipispisa)
        {


            if (_report != null && tipispisa == 1)
            {

                //   _report.CreateDocument();

                PrinterSettings settings = new PrinterSettings();
                //old
                string printertoprint = PlaPrinterUtility.GetPrinterToPrinter();
                settings.PrinterName = printertoprint;
                _report.PrinterName = printertoprint;
                //_report.PrintingSystem.PageSettings.PrinterName = printertoprint;
                _report.PrintingSystem.StartPrint += PrintingSystem_StartPrint;

                _report.PrintingSystem.ShowMarginsWarning = false;
                _report.PaperKind = settings.DefaultPageSettings.PaperSize.Kind;
                _report.PaperName = settings.DefaultPageSettings.PaperSize.PaperName;

                //_report.PageSize
                //_report.PageSize.Height = settings.DefaultPageSettings.PaperSize.Height;
                //_report.PageSize.Width = settings.DefaultPageSettings.PaperSize.Width;
                _report.PrintingSystem.PageSettings.PaperKind = settings.DefaultPageSettings.PaperSize.Kind;
                _report.PrintingSystem.PageSettings.PaperName = settings.DefaultPageSettings.PaperSize.PaperName;
                PageSettings _pageSettings = new PageSettings(settings);
                bool landscape = _report.Landscape;
                       _pageSettings.Landscape = _report.Landscape;
                 Margins _minmarg = new Margins(0,0,0,0);
                        if (settings.DefaultPageSettings.Landscape)
                        {
                            _minmarg = GetMinMarginsMojLandS(_pageSettings);
                            
                        }else
                        {
                           _minmarg = GetMinMarginsMoj(_pageSettings);
                        }
                        _pageSettings.Margins = _minmarg;
                        _report.PrintingSystem.PageSettings.Assign(_pageSettings,new Margins(0,0,0,0));
                        _report.PrintingSystem.PageSettings.Landscape = landscape;
                        _report.Margins = _pageSettings.Margins;
/*
                if (_report.Landscape)
                {
                       if (settings.LandscapeAngle == 90)
                            {
                                _pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Left = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Height - settings.DefaultPageSettings.PrintableArea.Height - settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Bottom  = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Width - settings.DefaultPageSettings.PrintableArea.Width - settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;    
                                _pageSettings.Margins.Right = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
 
                            }
                            if (settings.LandscapeAngle == 270)
                            {
                                _pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Width - settings.DefaultPageSettings.PrintableArea.Width - settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;    
                                _pageSettings.Margins.Left = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Bottom = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Right = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Height - settings.DefaultPageSettings.PrintableArea.Height - settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                            }

                }
                else
                {
                    //_pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top);
                    //_pageSettings.Margins.Left =
                    //    (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left);
                    //_pageSettings.Margins.Bottom =
                    //    (int)
                    //    Math.Round(settings.DefaultPageSettings.PaperSize.Height -
                    //               settings.DefaultPageSettings.PrintableArea.Height -
                    //               settings.DefaultPageSettings.PrintableArea.Top);
                    //_pageSettings.Margins.Right =
                    //    (int)
                    //    Math.Round(settings.DefaultPageSettings.PaperSize.Width -
                    //               settings.DefaultPageSettings.PrintableArea.Width -
                    //               settings.DefaultPageSettings.PrintableArea.Left);
                      _pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                            _pageSettings.Margins.Left = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;
                            _pageSettings.Margins.Bottom = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Height - settings.DefaultPageSettings.PrintableArea.Height - settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                            _pageSettings.Margins.Right = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Width - settings.DefaultPageSettings.PrintableArea.Width - settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;   
                }
              
                _report.PrintingSystem.PageSettings.Assign(_pageSettings);
                _report.PrintingSystem.PageSettings.Landscape = landscape;
                _report.Margins = _pageSettings.Margins;
 * */
                  _last = _pageSettings;
                _report.CreateDocument();
               // ((PrintingSystem)_report.PrintingSystem).Print(printertoprint);
                _report.Print(printertoprint);
            }
            if (tipispisa == 0)
                workitem.EventTopics[EventTopicNames.PrikaziIzvjestaj].Fire("PlaIzvjestaj",
                                                                            new EventArgs<object, string>(
                                                                                _report, String.Format(_naziv)),
                                                                            workitem, PublicationScope.Global);

        }
        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {
            e.PrintDocument.PrinterSettings.Copies = Convert.ToInt16(PlaPrinterUtility.BrojKopija);
        }
        internal static Margins GetMinMarginsMoj(PageSettings pageSettings) {
			PrinterSettings printerSettings = pageSettings.PrinterSettings;
			if(!String.IsNullOrEmpty(printerSettings.PrinterName)) {
				IntPtr hdc = Win32.CreateDC(null, printerSettings.PrinterName, IntPtr.Zero, IntPtr.Zero);
				if(hdc != IntPtr.Zero)
					try {
						using(Graphics gr = Graphics.FromHdc(hdc)) {
							Margins minMargins = DeviceCaps.GetMinMargins(gr);
							if (pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 90)
							    return (ConvertToLandscape90(minMargins));
                            if (pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 270)
							    return (ConvertToLandscape270(minMargins));
                            return minMargins;
						}
					} finally {
						Win32.DeleteDC(hdc);
					}
			}
			return null;
		}
        internal static Margins GetMinMarginsMojLandS(PageSettings pageSettings) {
			PrinterSettings printerSettings = pageSettings.PrinterSettings;
			if(!String.IsNullOrEmpty(printerSettings.PrinterName)) {
				IntPtr hdc = Win32.CreateDC(null, printerSettings.PrinterName, IntPtr.Zero, IntPtr.Zero);
				if(hdc != IntPtr.Zero)
					try {
						using(Graphics gr = Graphics.FromHdc(hdc)) {
							Margins minMargins = DeviceCaps.GetMinMargins(gr);
							if (!pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 90)
							    return (ConvertToLandscape90(minMargins));
                            if (!pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 270)
							    return (ConvertToLandscape270(minMargins));
                            return minMargins;
						}
					} finally {
						Win32.DeleteDC(hdc);
					}
			}
			return null;
		}
        static Margins ConvertToLandscape90(Margins margins) {
			return new Margins(margins.Bottom,margins.Top,margins.Right,margins.Left);
		}
        static Margins ConvertToLandscape270(Margins margins) {
			return new Margins(margins.Top, margins.Bottom, margins.Right, margins.Left);
		}
    }
}


